package com.rockchips.mediacenter.modle.task;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.rockchips.mediacenter.bean.FileInfo;
import com.rockchips.mediacenter.data.ConstData;
import com.rockchips.mediacenter.utils.MediaFileUtils;

import android.os.AsyncTask;

/**
 * @author GaoFei
 * 文件搜索异步块
 */
public class FileSearchTask extends AsyncTask<Object, Integer, List<FileInfo>>{
	public interface Callback{
		void OnFinished(List<FileInfo> resultFileInfos);
	}
	private List<FileInfo> mAllFileInfos;
	private Callback mCallback;
	public FileSearchTask(List<FileInfo> allFileInfos, Callback callback){
		mAllFileInfos = allFileInfos;
		mCallback = callback;
	}
	
	@Override
	protected List<FileInfo> doInBackground(Object... params) {
		List<FileInfo> resultInfos = new ArrayList<>();
		String searchText = (String)params[0];
                 for(FileInfo itemFileInfo : mAllFileInfos){
                       File file=new File(itemFileInfo.getPath());
                       if(file.isDirectory())
                       traverseFolder(file.getAbsolutePath(),resultInfos,searchText);
                       else {
                               String fileName = itemFileInfo.getName();
                               List<String> pinYinHeads = MediaFileUtils.getHeadPinyins(fileName);
                               if(fileName.toLowerCase().contains(searchText) 
                                              || MediaFileUtils.getFullPinYin(fileName).toLowerCase().contains(searchText)){
                                      itemFileInfo.setType(MediaFileUtils.getMediaTypeFromFile(file));
                                       resultInfos.add(itemFileInfo);
                               }else if(pinYinHeads != null && pinYinHeads.size() > 0){
                                       for(String pinYinHead : pinYinHeads){
                                               if(pinYinHead.toLowerCase().contains(searchText)){
                                                       itemFileInfo.setType(MediaFileUtils.getMediaTypeFromFile(file));
                                                       resultInfos.add(itemFileInfo);
                                                       break;
                                               }
                                       }
                              }
                       }
                }

		return resultInfos;
	}
	
	@Override
	protected void onPostExecute(List<FileInfo> resultFileInfos) {
		mCallback.OnFinished(resultFileInfos);
	}

        private void traverseFolder(String path,List<FileInfo> resultInfos,String searchText) {
               File file = new File(path);
        if (file.exists()) {
               String fileName = file.getName();
               FileInfo itemFileInfo= new FileInfo();
//             itemFileInfo.setDeviceID(device.getDeviceID());
               itemFileInfo.setModifyTime(file.lastModified());
                       itemFileInfo.setName(file.getName());
                       itemFileInfo.setPath(file.getPath());
                       itemFileInfo.setParentPath(path);
                       List<String> pinYinHeads = MediaFileUtils.getHeadPinyins(fileName);
                       if(fileName.toLowerCase().contains(searchText) 
                                        || MediaFileUtils.getFullPinYin(fileName).toLowerCase().contains(searchText)){
                               itemFileInfo.setType(ConstData.MediaType.FOLDER);
                               itemFileInfo.setChildCount(file.listFiles().length);
                                resultInfos.add(itemFileInfo);
                       }else if(pinYinHeads != null && pinYinHeads.size() > 0){
                               for(String pinYinHead : pinYinHeads){
                                        if(pinYinHead.toLowerCase().contains(searchText)){
                                               itemFileInfo.setChildCount(file.listFiles().length);
                                               itemFileInfo.setType(ConstData.MediaType.FOLDER);
                                               resultInfos.add(itemFileInfo);
                                               break;
                                       }
                               }
                        }
            File[] files = file.listFiles();
            if (files.length == 0) {
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        traverseFolder(file2.getAbsolutePath(),resultInfos,searchText);
                    } else {
                       String fileName2 = file2.getName();
                       FileInfo itemFileInfo2= new FileInfo();
//                     itemFileInfo.setDeviceID(device.getDeviceID());
                       itemFileInfo2.setModifyTime(file2.lastModified());
                       itemFileInfo2.setName(file2.getName());
                       itemFileInfo2.setPath(file2.getPath());
                       itemFileInfo2.setParentPath(file.getPath());    
                               List<String> pinYinHeads2 = MediaFileUtils.getHeadPinyins(fileName2);
                               if(fileName2.toLowerCase().contains(searchText) 
                                               || MediaFileUtils.getFullPinYin(fileName2).toLowerCase().contains(searchText)){
                                       itemFileInfo2.setType(MediaFileUtils.getMediaTypeFromFile(file2));
                                       resultInfos.add(itemFileInfo2);
                               }else if(pinYinHeads2 != null && pinYinHeads2.size() > 0){
                                       for(String pinYinHead : pinYinHeads2){
                                               if(pinYinHead.toLowerCase().contains(searchText)){
                                                       itemFileInfo2.setType(MediaFileUtils.getMediaTypeFromFile(file2));
                                                       resultInfos.add(itemFileInfo2);
                                                       break;
                                               }
                                       }
                               }
                    }
                }
            }
        } 
       }


}
